package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Diseasecategory;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.vo.DiseasecategoryVo;
import com.jrtc.service.DiseasecategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 疾病分类表(Diseasecategory)表控制层
 *
 * @author makejava
 * @since 2022-03-21 17:19:03
 */
@RestController
@RequestMapping("diseasecategory")
public class DiseasecategoryController {
    /**
     * 服务对象
     */
    @Resource
    private DiseasecategoryService diseasecategoryService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Diseasecategory selectOne(Integer id) {
        return this.diseasecategoryService.queryById(id);
    }
    
    @RequestMapping("getDiseasecategoryList")
    public ResultDTO getDiseasecategoryList(@RequestBody Diseasecategory diseasecategory)
    {
        IPage<Diseasecategory> ipage = diseasecategoryService.queryDiseasecategoryList(diseasecategory);
        return ResultDTOBuilder.success(ipage);
    }

    @RequestMapping("insertDiseasecategory")
    public ResultDTO insertDiseasecategory(@RequestBody Diseasecategory diseasecategory)
    {
        Diseasecategory tmpDiseasecategory = diseasecategoryService.insert(diseasecategory);
        if (tmpDiseasecategory != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getDiseasecategoryById")
    public ResultDTO getDiseasecategoryById(@RequestBody Map<String, Integer> map)
    {
        int diseasecategoryId = map.get("diseasecategoryId");
        Diseasecategory diseasecategory = diseasecategoryService.queryById(diseasecategoryId);
        if (diseasecategory==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(diseasecategory);
    }

    @RequestMapping("updateDiseasecategory")
    public ResultDTO updateDiseasecategory(@RequestBody Diseasecategory diseasecategory)
    {
        if (diseasecategory==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (diseasecategoryService.update(diseasecategory)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deleteDiseasecategory")
    public ResultDTO deleteDiseasecategory(@RequestBody Map<String,Integer> map)
    {
        int diseasecategoryId = map.get("diseasecategoryId");
        if (diseasecategoryService.deleteById(diseasecategoryId))
        {
            return ResultDTOBuilder.success("删除成功！");
        }
        return ResultDTOBuilder.failureMsg("删除失败！");
    }

    /**
     * 查询疾病分类信息
     * */
    @PostMapping("/seleteDiseasecategory")
    public ResultDTO seleteDiseasecategory(@RequestBody Map<String, Object> map){
        Integer diseasecategoryId = null;
        if (map.get("diseasecategoryId") != null && !map.get("diseasecategoryId").equals("")){
            diseasecategoryId = Integer.parseInt(map.get("diseasecategoryId").toString());
        }
        List<DiseasecategoryVo> diseasecategoryVoList;
        try {
            diseasecategoryVoList = diseasecategoryService.seleteDiseasecategory(diseasecategoryId);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }
        if (diseasecategoryVoList != null && diseasecategoryVoList.size() > 0){
            return ResultDTOBuilder.success(diseasecategoryVoList);
        }
        else {
            return ResultDTOBuilder.failureMsg("未查到疾病分类信息");
        }
    }

    /**
     * 疾病分类信息列表
     * */
    @PostMapping("/diseasecategoryList")
    public ResultDTO diseasecategoryList(){
        List<Diseasecategory> diseasecategories = diseasecategoryService.diseasecategoryList();
        return ResultDTOBuilder.success(diseasecategories);
    }
}